$(function() {
  $(':button#btnpack').bind('click', function() {
    $.getJSON('/_splitbox', {
      oldbox: $('input[name="oldbox"]').val(),
      boxid: $('input[name="boxinput"]').val(),
      baseqty: $('input[name="baseqty"]').val(),
      ssn: $('input[name="ssn"]').val(),
      date: new Date().getTime()+Math.random()
    }, function(data) {
      if(data.result == 'RELOGIN')
      {
        location.href = "/login";
      }
      else
      {
        $("#result").text(data.result);
        if($("#result").text() == "")
        {
          var pqty = data.packedqty;
          var bqty = parseInt($('input[name="baseqty"]').val());
          var d = new Date().format("yyyy-MM-dd hh:mm:ss");
          var s_ssn = ssn.value;
          var t_box = $("#oldbox").val();
          var s_box = $("#boxinput").val();

          if(pqty < bqty)
          {
            document.getElementById("packqty").innerText = String(pqty);
          }
          else
          {
            document.getElementById("baseqty").innerText = "0";
            document.getElementById("packqty").innerText = "0";
            document.getElementById("boxinput").innerText = "";
          }            
          $("#flowdetail").val(d + "   Move " + s_ssn + " From: " + t_box + " To: " + s_box + " finished.\r" + $("#flowdetail").text());  
        }
        ssn.value = "";
      }
    });
    return false;
  });
})

function txtKeyDown()
{
  if(event.keyCode == 13)
  {
    btnpack.click();
    ssn.focus();
  }
}
